<?php
/**
 * Created by PhpStorm.
 * User: 杨永安
 * Date: 2017/4/18
 * Time: 22:46
 */

namespace app\api\controller;


use app\api\model\Order as OrderModel;
use think\Controller;

class Ship extends Controller
{

    function order_list($offset = 0, $status = 'all')
    {
        $order = new OrderModel();

        if ($status == 'all') {
            $res['msg'] = $order->limit($offset, 10)->order("Id", "DESC")->select();
        } else {

            if ($status > 3) {

                $res['msg'] = $order->where('Status', $status)->where("ShipUserId", uid())->order("Id", "DESC")->limit($offset, 10)->select();
            } else {

                $res['msg'] = $order->where('Status', $status)->order("Id", "DESC")->limit($offset, 10)->select();
            }

        }

        foreach ($res['msg'] as $k => $v) {
            $res['msg'][$k]['Name'] = $v->address()['Name'];
            $res['msg'][$k]['Ad'] = $v->address()['Address'];
            $res['msg'][$k]['cost'] = $v->cost($v['Id']);

            $res['msg'][$k]['from_loc'] = db('shop')->where('Id', $v['ShopId'])->field('Lon,Lat')->find();
            $res['msg'][$k]['to_loc'] = db('user_address')->where('Id', $v['Address'])->field('Lon,Lat')->find();


            unset($res['msg'][$k]['VerifyCode']);
        }


        $res['code'] = 1;

        return json($res);

    }


    function grab($order_id)
    {

        if (isShiper()) {
            $order = db('order')->where('Id', $order_id)->find();
            if ($order) {
                if ($order['Status'] == 3) {
                    $set = db('order')->where('Id', $order_id)->update(["Status" => 4, "ShipUserId" => uid(), "ShipTime" => time()]);


                    if ($set) {


                        $content['content'] = "开始配送";
                        $content['extra'] = "SHIP_START";

                        publishPrivate($order['UserId'], $content, uid());



                        $res['code'] = 1;
                        $res['msg'] = "抢单成功!";
                    } else {

                        $res['code'] = -4;
                        $res['msg'] = "抢单失败";
                    }


                } else {

                    $res['code'] = -3;
                    $res['msg'] = "哎呀~~~下手晚了,被人抢走了";
                }


            } else {
                $res['code'] = -1;
                $res['msg'] = "订单找不到";
            }
        } else {

            $res['code'] = -2;
            $res['msg'] = "您没有抢单资格";
        }


        return json($res);
    }


    function confirm($order_id = 0, $verify_code = 0)
    {
        if ($order_id && $verify_code) {
            if (isShiper()) {
                $order = db("order")->where("Id", $order_id)->find();
                if ($order) {
                    if ($order['Status'] == 4) {
                        if ($order['ShipUserId'] == uid()) {
                            if ($order['VerifyCode'] == $verify_code) {
                                $set = db('order')->where("Id", $order_id)->update(["Status" => 5, "EndTime" => time()]);
                                if ($set) {
                                    $res['code'] = 1;
                                    $res['msg'] = "确认成功!";



                                    $content['content'] = "已经确认,订单结束";
                                    $content['extra'] = "SHIP_END";

                                    publishPrivate($order['UserId'], $content, uid());




                                } else {
                                    $res['code'] = -7;
                                    $res['msg'] = "确认失败";
                                }
                            } else {
                                $res['code'] = -6;
                                $res['msg'] = "验证码不正确";
                            }
                        } else {
                            $res['code'] = -5;
                            $res['msg'] = "订单不属于您";
                        }
                    } else {
                        $res['code'] = -4;
                        $res['msg'] = "订单状态不正确";
                    }
                } else {
                    $res['code'] = -3;
                    $res['msg'] = "订单找不到";
                }
            } else {
                $res['code'] = -2;
                $res['msg'] = "您没有抢单资格";
            }
        } else {
            $res['code'] = -1;
            $res['msg'] = "参数不正确";
        }
        return json($res);
    }


}